﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using fingerWinForm.SVM;
using fingerWinForm.Utilities;

namespace fingerWinForm.GUI
{
    public partial class SVMWindow : Form
    {
        public struct SVMParameters
        {
            public double gammaStart;
            public double gammaEnd;
            public double gammaDecrement;
            public double cStart;
            public double cEnd;
            public double cIncrement;
            public MainWindow.TestName testName;
            //public ProgressBar pBar;
        }

        private SVMManager svmManager;
        private SVMParameters svmParameters;
        public SVMWindow()
        {
            InitializeComponent();
            svmManager = new SVMManager();
            svmParameters = new SVMParameters();
            svmManager.m_updatePrograssBar += new SVMManager.updatePrograssBar(prograssBarUpdater);
        }


        private void clearAll()
        {
            gammaDecrementTextBox.Clear();
            gammaEndTextBox.Clear();
            gammaStartTextBox.Clear();
            cEndTextBox.Clear();
            cIncrementTextBox.Clear();
            cStartTextBox.Clear();
            testNamesComboBox.Text = "";
            progressBar.Value = 0;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            clearAll();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            clearAll();
            this.Close();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            if (CheckInput.isEmpty(gammaDecrementTextBox.Text) ||
                CheckInput.isEmpty(gammaEndTextBox.Text) ||
                CheckInput.isEmpty(gammaStartTextBox.Text) ||
                CheckInput.isEmpty(cEndTextBox.Text) ||
                CheckInput.isEmpty(cIncrementTextBox.Text) ||
                CheckInput.isEmpty(cStartTextBox.Text) ||
                CheckInput.isEmpty(testNamesComboBox.Text) )
            {
                MessageBox.Show("Please fill all the parameters for SVM", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else if (Convert.ToInt32(cStartTextBox.Text) <= 0)
            {
                MessageBox.Show("C start parameter must be greater then 0", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else if (Convert.ToDouble(gammaStartTextBox.Text) < Convert.ToDouble(gammaEndTextBox.Text) )
            {
                MessageBox.Show("Gamma start parameter must be greater then Gamma end parameter", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else if (Convert.ToDouble(cEndTextBox.Text) < Convert.ToDouble(cStartTextBox.Text))
            {
                MessageBox.Show("C start parameter must be smaller then C end parameter", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else if (Convert.ToDouble(cEndTextBox.Text) - Convert.ToDouble(cStartTextBox.Text) == 0)
                     
            {
                MessageBox.Show("Difference between C starting value and C ending value cannot be 0. Please change the values ", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else if (Convert.ToDouble(gammaStartTextBox.Text) - Convert.ToDouble(gammaEndTextBox.Text) == 0)
            {
                MessageBox.Show("Difference between Gamma starting value and Gamma ending value cannot be 0. Please change the values ", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                svmParameters.gammaStart = Convert.ToDouble(gammaStartTextBox.Text);
                svmParameters.gammaEnd = Convert.ToDouble(gammaEndTextBox.Text);
                svmParameters.gammaDecrement = Convert.ToDouble(gammaDecrementTextBox.Text);
                svmParameters.cStart = Convert.ToInt32(cStartTextBox.Text);
                svmParameters.cEnd = Convert.ToInt32(cEndTextBox.Text);
                svmParameters.cIncrement = Convert.ToInt32(cIncrementTextBox.Text);
                progressBar.Value = 0;
                if (checkBox.Checked)
                {
                    svmManager.setNumOfClasses(3);
                }
                else
                {
                    svmManager.setNumOfClasses(5);
                }
                if (testNamesComboBox.Text == "23 - Finger Taps")
                {
                    svmParameters.testName = MainWindow.TestName.FINGER_TAPS;
                    svmManager.createListOfFilesForTrainAndPredict(svmParameters);
                }
                if (testNamesComboBox.Text == "24 - Hand Movements")
                {
                    svmParameters.testName = MainWindow.TestName.HAND_MOVEMENTS;
                    svmManager.createListOfFilesForTrainAndPredict(svmParameters);
                }
                progressBar.Maximum = (Convert.ToInt32(((svmParameters.gammaStart - svmParameters.gammaEnd) /
                                                        (svmParameters.gammaDecrement))) *
                                      (Convert.ToInt32((svmParameters.cEnd - svmParameters.cStart) / svmParameters.cIncrement)));
                System.Threading.Thread oThread = new System.Threading.Thread(svmManager.crossValidation);
                oThread.Start();
            }
            
        }

        public void prograssBarUpdater(int progressBarStep)
        {
            if (InvokeRequired == true)
            {
                BeginInvoke(new MethodInvoker(() => { prograssBarUpdater(progressBarStep); }));
            }
            else
            {
                if (progressBar.Value == progressBar.Maximum)
                {
                    return;
                }
                else
                {
                    progressBar.Value += progressBarStep;  
                }
                
            }
        }
    }
}
